package mydessertstore.web.servlet;

import mydessertstore.domain.Account;
import mydessertstore.domain.Order;
import mydessertstore.service.LogService;
import mydessertstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.IconUIResource;
import java.io.IOException;
import java.util.List;

public class ListOrdersFormServlet extends HttpServlet {

    private static final String LIST_ORDERS_FORM = "/WEB-INF/jsp/order/listOrders.jsp";

    private String username;
    private OrderService orderService;
    private List<Order> orderList;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        username = req.getParameter("username");
        orderService= new OrderService();
        orderList = orderService.getOrdersByUsername(username);

        HttpSession session = req.getSession();
        session.setAttribute("orderList" , orderList);

        System.out.println(orderList);

        //日志功能
        Account account=(Account) session.getAttribute("account");
        if(account!=null) {

            LogService logService = new LogService();

            String logInfo = logService.logInfo(" ") + " 查看订单 " ;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        else{
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + " 查看订单 " ;
            logService.insertLogInfo("未登录用户", logInfo);
        }
        //日志结束

        req.getRequestDispatcher(LIST_ORDERS_FORM).forward(req,resp);
    }
}
